/**
 * Title: RoleController.java
 * @author wujin
 * @date 2019年12月30日
 * @version 1.0
 */
package com.pm.controller.task;

import com.github.pagehelper.PageInfo;
import com.pm.common.Pages;
import com.pm.common.Response;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.CommonException;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.dto.task.TaskQuarterScoreTree;
import com.pm.entity.po.task.TaskCompletionDegree;
import com.pm.entity.vo.task.TeamScoreRankVo;
import com.pm.service.task.TaskCompletionDegreeService;
import com.pm.utils.DateUtil;
import com.pm.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Title: RoleController
 *
 * @author wujin
 * @date 2019年12月30日
 */
@Api(tags = "任务完成度")
@ApiModel
@UserLoginToken
@RestController
@RequestMapping("/taskDegree")
public class TaskCompletDegreeController {

    @Autowired
    private TaskCompletionDegreeService taskCompletionDegreeService;

    @ApiOperation("单个对象查询接口")
    @PostMapping("/load")
    @ResponseBody
    public Response<TaskCompletionDegree> load(TaskCompletionDegree taskCompletionDegree) {
        Response<TaskCompletionDegree> response = null;
        TaskCompletionDegree result = taskCompletionDegreeService.load(taskCompletionDegree);
        response = new Response<TaskCompletionDegree>(ExpCodeState.query_data_success, result);
        return response;
    }

    @ApiOperation("全部列表查询接口")
    @PostMapping("/getList")
    @ResponseBody
    @MyLog(value = "查询任务任务完成度列表")
    public Response<List<TaskCompletionDegree>> getList(TaskCompletionDegree taskCompletionDegree) {
        List<TaskCompletionDegree> result = taskCompletionDegreeService.getList(taskCompletionDegree);
        Response<List<TaskCompletionDegree>> res = new Response<List<TaskCompletionDegree>>(
                ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("分页查询接口")
    @PostMapping("/getListByPage/{pageNum}/{pageSize}")
    @ResponseBody
    @MyLog(value = "查询任务任务完成度列表")
    public Response<PageInfo<TaskCompletionDegree>> getListByPage(TaskCompletionDegree taskCompletionDegree,
                                                                  Pages page) {
        PageInfo<TaskCompletionDegree> result = taskCompletionDegreeService.getListByPage(taskCompletionDegree,
                page);
        Response<PageInfo<TaskCompletionDegree>> res = new Response<PageInfo<TaskCompletionDegree>>(
                ExpCodeState.query_data_success, result);
        return res;
    }


    @ApiOperation("更新接口")
    @PostMapping("/update")
    @ResponseBody
    @MyLog(value = "编辑任务完成度信息")
    public Response<Integer> update(@RequestBody TaskCompletionDegree taskCompletionDegree) {
        Integer result = taskCompletionDegreeService.update(taskCompletionDegree);
        Response<Integer> res = new Response<Integer>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("新增接口")
    @PostMapping("/add")
    @ResponseBody
    @MyLog(value = "新增任务完成度信息")
    public Response<Integer> add(@RequestBody TaskCompletionDegree taskCompletionDegree) {
        Integer result = taskCompletionDegreeService.add(taskCompletionDegree);
        Response<Integer> response = null;
        if (result > 0) {
            response = new Response<>(ExpCodeState.add_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.add_data_failed);
        }
        return response;
    }

    @ApiOperation("删除接口")
    @PostMapping("/remove")
    @ResponseBody
    @MyLog(value = "删除任务完成度信息")
    public Response<Integer> remove(@RequestBody TaskCompletionDegree taskCompletionDegree) {
        Response<Integer> response = null;
        Integer result = taskCompletionDegreeService.remove(taskCompletionDegree);
        if (result > 0) {
            response = new Response<>(ExpCodeState.remove_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.remove_data_failed);
        }
        return response;
    }

    @ApiOperation("查询得分总和")
    @PostMapping("/getAllScore")
    @ResponseBody
    @Transactional(rollbackFor = CommonException.class)
    @MyLog(value = "查询重点工作得分")
    public Response<List<TaskQuarterScoreTree>> getAllScore(@RequestBody TeamScoreRankVo teamScoreRankVo) {
        if (StringUtils.isBlank(teamScoreRankVo.getYear())) {
            // 默认当前年
            teamScoreRankVo.setYear(DateUtil.getYear());
        }
        List<TaskQuarterScoreTree> result = taskCompletionDegreeService.getAllScore(teamScoreRankVo);
        Response<List<TaskQuarterScoreTree>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("查询领导小组得分排名")
    @PostMapping("/getTeamScoreRank")
    @ResponseBody
    @MyLog(value = "查询领导小组得分排名")
    public Response<List<TaskCompletionDegree>> getTeamScoreRank(@RequestBody TeamScoreRankVo teamScoreRankVo) {
        if (teamScoreRankVo.getYear() == null) {
            // 默认当前年
            teamScoreRankVo.setYear(DateUtil.getYear(new Date()));
        }
        List<TaskCompletionDegree> result = taskCompletionDegreeService.getTeamScoreRank(teamScoreRankVo);
        Response<List<TaskCompletionDegree>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }
}
